{include file="public/head"/}
<div class="layui-fluid" id="LAY-app-message">
    <div class="layui-card">
        <div class="layui-card-header">单页信息列表</div>
        <div class="layui-card-body">
            <table class="layui-table" id="list" lay-filter="list"></table>
        </div>
    </div>
</div>
{include file="public/foot"/}
<script type="text/html" id="title">
    {{d.title}}
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="action">
    {{# if(d.is_page==1){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="page_edit">内容</a>
    {{# } }}
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
</script>
<script>
    layui.config({
        base: '__LAYDMIN__/',{if config('app.app_debug')}version: true{/if}
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','admin', 'table', 'util','upload'],function(){
		var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element, form = layui.form,util = layui.util, upload = layui.upload;
		var tableLoadDiv = window.top.layer.load(0,{shade:[0.15, '#000']});//加载层,table.render完成时需要关闭
		//全部数据
		tableIn = table.render({
			elem: '#list',
			url: '{:url(SYS_PATH.'/'.CONTROLLER.'/index')}',
			method: 'post',
			cols: [[
				{field: 'id', title: 'ID', width: 80, fixed: true},
				{field: 'title', title: '栏目名称', width: 400,templet: '#title'},
				{field: 'update_time', title: '修改时间', width: 160,templet: function(d){
							return util.toDateString(d.update_time*1000,'yyyy-MM-dd HH:mm:ss');
				}},
				{field: 'status', align: 'center', title: '状态', width: 100, toolbar: '#status'},
				{field: 'sort', align: 'center', title: '排序', width: 80, edit:'text'},
				{width: 150, title:'操作',align: 'center', toolbar: '#action'}
			]],
			done:function (res) {   //返回数据执行回调函数
			    	window.top.layer.close(tableLoadDiv);    //返回数据关闭loading
			}				
		});
		
		$('.clt-auth-btns .layui-btn').on('click', function () {
			var type = $(this).data('type');
			layui.layerOpen("{:url(SYS_PATH.'/'.CONTROLLER.'/add')}");
		});		
		
		
		table.on('edit(list)', function(obj){
			var value = obj.value //得到修改后的值
				,field = obj.field; //得到字段
			admin.req({
				url: '{:url(SYS_PATH.'/'.CONTROLLER.'/listEdit')}?id='+obj.data.id+'&'+field+'='+value
				, success: function (res) {
					if(res.code==0){
						window.top.layer.msg(res.msg,{time:1000,icon:1,shade:[0.5,'#000']});
					}
					tableIn.reload();
				}
			});
		});		

		
		
	   //监听工具条
		table.on('tool(list)', function (obj) {
			var data = obj.data;
			var id = data.id;
			if (obj.event === 'del') {
				var uri="{:url(SYS_PATH.'/'.CONTROLLER.'/doDelete')}"
				layui.doDelete(uri,"确定要删除吗？",id,obj);//单个删除接口
			}else if (obj.event === 'edit') {
				layui.layerOpen("{:url(SYS_PATH.'/'.CONTROLLER.'/edit')}?id="+id);
			}
		});		
		
		//改变状态
		form.on('switch(status)', function (obj) {
			var id = this.value;
			var status = obj.elem.checked === true ? 1 : 0;
			admin.req({
				url: '{:url(SYS_PATH.'/'.CONTROLLER.'/doStatus')}',
				type:'post',
				data: {'id': id, 'status': status},
				success: function (res) {
					if (res.code==0) {
						window.top.layer.msg(res.msg, {time: 1000, icon: 1,shade:[0.5,'#000']});
					} else {
						window.top.layer.msg(res.msg, {time: 1000, icon: 2,shade:[0.5,'#000']});
						if (obj.elem.checked == true) {
							$(obj.elem).prop('checked', false);
						} else {
							$(obj.elem).prop('checked', true);
						}
						form.render();						
					}
				}
			});
		});		
		
	});
</script>
</body>
</html>